<?php

namespace app\server;

use fast\Http;
use fast\Random;

class WechartServer
{
    public  $appid;
    private $secret;
    private $token;
    private $redirect_uri;
    private $state;
    public function __construct()
    {
        $this->appid=config('wechart.appid');
        $this->secret=config("wechart.secret");
        $this->redirect_uri=config('wechart.redirect_uri');
        $this->state=cookie('state')?cookie('state'):uniqid();
    }
    public function oauth()
    {
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".urlencode($this->redirect_uri)."&response_type=code&scope=snsapi_userinfo&state=".$this->state."#wechat_redirect&forcePopup=true";
        header("location:".$url);
    }
    public function getToken($option=['code'=>"","state"=>""])
    {
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
        $result=Http::get($url);
        return $result;
    }
    public function getUserInfo($option)
    {
        $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$option['access_toke']."&openid=".$option['openid']."&lang=zh_CN";
    }
}